package com.cndw.kungfu.event;

import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

public class RobotHandler implements Runnable {
	
	private Scene scene;
	private Player player;
	
	public static boolean isover=false;
	
	public RobotHandler(int sceneId,long playerId){
		
		scene = Context.getSceneServer().getSceneById(sceneId);
		
		player = Context.getPlayerServer().getByPlayerId(playerId);		
		
		if (player == null || player.isOnline()) {
			
		}
		
	}

	public boolean isSuc(){
		if (player == null || player.isOnline()) {
			
			return false;
		}
		return true;
	}
	
	@Override
	public void run() {
		if (player == null || player.isOnline()) {
			return ;
		}
		if (isover) {
			
			return;
		}
		if (player.getSubLine() == 0 || player.getSceneId() == 0) {
			
			player.setSubLine(1);
			
			scene.makeSubLine(player);
			player.setSceneId(scene.getId());
			//player.commitSync();
			scene.broadcast(player, player.toScenePlayerVO(), ConstantsRadio.sceneEnter);
		}else {
			if (NumberProcesser.isWinning(2)) {
				
				int subLine = player.getSubLine();
				
				scene.removePlayerId(player.getPlayerId(),subLine);
				
				
				player.setSubLine(0);
				//player.commitSync();
				
				scene.broadcast(player, player.getPlayerId(), ConstantsRadio.sceneExit);
				return;
			}			
		}
		
		
		int x = NumberProcesser.mtRand(10, 2500);
		int y = NumberProcesser.mtRand(100, 600);
		
		player.setTx(x);
		player.setTy(y);
		player.setTz(TimeProcesser.getUnixTime());
		//player.commitSync();
		
		scene.broadcast(player, new Object[]{player.getPlayerId(),x,y}, ConstantsRadio.sceneMove);
		
		
		
	}
	
}
